#!/Activeperl/bin/perl

use 5.010;
use strict;
use warnings;

# initialization

use FindBin('$Bin'); 
   # so $Bin is the location of the very file we're in now

use lib $Bin; 
   # there are few enough files that it makes sense to keep
   # main program and library in the same directory

# libraries dependent on $Bin

use Actium::Sorting (qw(sortbyline));
use FPMerge qw(FPread FPread_simple);
use Myopts;
use Skeddir;
use Linelist('linelist');

my %options;
Myopts::options (\%options, Skeddir::options(), 'quiet!');
# command line options in %options;

my $signup;
$signup = (Skeddir::change (\%options))[2];
# Takes the necessary options to change directories, plus 'quiet', and
# then changes directories to the "Skeds" base directory.

my $current_version = '13';

use autodie;

open my $date , "<effectivedate.txt" ;

our $effdate = scalar <$date>;
close $date;

chomp $effdate;
$effdate =~ s/\r//g;

my (@lines, %lines);
FPread_simple ("Lines.csv" , \@lines, \%lines, 'Line');

my %desc_of;

my (%group);



for my $line (linelist()) {

   next if $line eq 'BSH';

   $desc_of{$line} = $lines{$line}{Description};

   given ($line) {
      when (/^6\d\d/) {
         push @{$group{Supplementary}}, $line;
      }
      when (/^8\d\d/) {
         push @{$group{AllNighter}}, $line;
      }
      when (/^[A-Z]/) {
         push @{$group{Transbay}}, $line;
      }
      default {
         push @{$group{Local}}, $line;
      }

   }
}

open my $out , '>' , 'linefinder.htm';

print $out header($effdate);

say $out "\n<!--\n    Do not edit this file! It is automatically generated from a program.\n-->";

foreach (qw/Local Transbay Supplementary AllNighter/) {
    # heading 
    my $pub = $_;
    $pub = 'All Nighter' if /AllNighter/;
    $pub .= ' Lines';
    

    say $out qq{<table><caption><a name="$_">$pub</a></caption>};

    say $out '<thead><tr><th>Lines</th><th>Description</th>';
    say $out '<th class="center">Links</th></thead>';
    say $out '<tbody>';

    foreach my $line (sortbyline @{$group{$_}} ) {

       say $out qq{<tr><td class="center" >$line</td>};
       say $out "<td>$desc_of{$line}</td>";
       say $out '<td class="center">';
       say $out qq{<a href="http://www.actransit.org/maps/maps_results.php?ms_view_type=2&maps_line=$line&version_id=$current_version&map_submit=Get+Map">Map</a>};
       say $out "<br><br>";
       say $out qq{<a href="http://www.actransit.org/maps/schedule_results.php?quick_line=$line&Go=Go&version_id=$current_version">Schedule</a>};
       say $out '</td></tr>';

    }
    say $out '</tbody></table>';

}

print $out FOOTER();

close $out;

###########################################################################
# END OF MAIN PROGRAM
###########################################################################

sub FOOTER {

return <<'EOF';
  <p> <strong>Find <a href="http://www.actransit.org/maps/index.php">maps 
    &amp; schedules</a> for these lines.</strong> Or call 511 and say 
    &quot;AC Transit&quot; for trip-planning assistance.</p>
  <p > <img align="right" src="/images/riderinfo/transbay/ACT_logo.jpg"> 
  </p>
</body>
</html>
EOF

}

sub header {

   my $effdate = shift;

return <<"EOF";
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
            "http://www.w3.org/TR/html4/loose.dtd">
<html lang="en" xml:lang="en" xmlns="http://www.w3.org/1999/xhtml">

<head>
<title>AC Transit Bus Line Descriptions</title>

<style type="text/css">
body {
    background-color: #fff;
    color: #000;
    font-family: arial, geneva, helvetica, sans-serif;
    text-align: left;
    font-size: 100%;

}    

table {
    border-collapse: collapse;
	border-style: ridge
    width: 90%;
	valign=top;
	vspace=3pt;
	text-display: automatic;
} 

caption {
    padding-top: 1.2em;
    text-align: left;
    font-weight: bold;
    font-size: 1.2em;
    padding-bottom: 0.2em;
}

th {
    background-color: silver;
    padding: 2pt;
    border: 1pt solid #006666;
    text-align: left;
}


td {
    padding: 2pt;
    border: 1pt solid #006666;
    text-align: left;
}

.center {
   text-align:center;
}
  
</style>
</head>

<body>

<h1> AC Transit Bus Line Descriptions </h1>

<p>Effective $effdate</p>

<h2>About Bus Line Numbers and Letters</h2>

<p>AC Transit's numbered lines serve the East Bay. </p>

<p>Lines 1–299 operate normal hours. Normal hours are, at a minimum,
the commute periods, 6 a.m. – 9 a.m. and 4 p.m. – 6 p.m., weekdays.
Almost all operate all day on weekdays and many operate weekday
evenings and weekends as well. Lines 200-299 serve the areas of
Fremont and Newark, while other lines serve other parts of the East
Bay from Richmond to Hayward.</p>

<p>Lines 300–399 do not operate during the commute period. They
operate at other times of the day: for example, mid-days only,
weekends only, or evenings only. Some lines operate only a few days
per week (for example, Tuesdays and Thursdays).</p>

<p>Lines 600–699 are timed to match the instruction hours of local
schools, and operate only when schools are in session. They may
have altered schedules when local schools have minimum day or
alternative schedules. These lines are open to all passengers at
regular fares.</p>

<p>Lines 800–899 are All-Nighter lines, operating from 1 a.m. – 5
a.m. daily. Some may operate somewhat earlier or later (especially
on weekends).</p>

<p>Lettered lines (A-Z) are Transbay routes, connecting the East
Bay to San Francisco or the Peninsula.</p>

<hr/>

<p><strong>Go to the <a href="http://www.actransit.org/maps/index.php">Maps &amp; Schedules</a> page.
</strong></p>

  <p > 
  <a href="#Local">Local Lines</a><br>
  <a href="#Transbay">Transbay Lines</a><br>
  <a href="#AllNighter">All Nighter Lines</a> <br>
  <a href="#Supplementary">Supplementary Lines</a><br>
</p>

<hr>
EOF

}
